import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号");
        int id = scanner.nextInt();
        System.out.println("请输入姓名");
        String name = scanner.next();
        //构建数据源
        MysqlDataSource dataSource =new MysqlDataSource() ;
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8");
        dataSource.setUser("root");
        dataSource.setPassword("354684");
        //连接数据库
        Connection connection =dataSource.getConnection();
        //构造sql语句
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.setString(2, name);
        //执行sql语句，返回值是“这次操作影响了几行”
        int n=statement.executeUpdate();
        System.out.println("n="+n);
        //释放必要的资源，关闭连接
        statement.close();//先创建的后关闭，后创建的先关闭
        connection.close();
    }
}
